import axios from '../axios'

/*
 * 心理测评答案管理模块
 */

// 调试输出
console.log('psyAnswer模块已加载');

// 所有API方法
const psyAnswer = {
  // 保存
  save(data) {
    console.log('save方法被调用');
    return axios({
      url: '/psyAnswer/save',
      method: 'post',
      data
    })
  },

  // 删除
  batchDelete(data) {
    console.log('batchDelete方法被调用');
    return axios({
      url: '/psyAnswer/delete',
      method: 'post',
      data
    })
  },

  // 分页查询
  findPage(data) {
    console.log('findPage方法被调用');
    
    // 确保data参数至少包含基本结构
    const requestData = data || { pageNum: 1, pageSize: 10 };
    
    // 确保params存在,且至少有一个占位参数
    if (!requestData.params || !Array.isArray(requestData.params) || requestData.params.length === 0) {
      requestData.params = [{ name: '_placeholder', value: null }];
    }
    
    return axios({
      url: '/psyAnswer/findPage',
      method: 'post',
      data: requestData
    })
  },

  // 查询当前用户的答案
  findByCurrentUser(termId) {
    console.log('findByCurrentUser方法被调用');
    return axios({
      url: '/psyAnswer/findByCurrentUser',
      method: 'get',
      params: {
        termId: termId
      }
    })
  },

  // 根据学生ID和学期查询答案
  findByStudentAndTerm(studentId, termId) {
    console.log('findByStudentAndTerm方法被调用');
    return axios({
      url: '/psyAnswer/findByStudentAndTerm',
      method: 'get',
      params: {
        studentId: studentId,
        termId: termId
      }
    })
  },

  // 保存答题
  saveAnswers(data, termId) {
    console.log('saveAnswers方法被调用');
    return axios({
      url: '/psyAnswer/saveAnswers',
      method: 'post',
      data: data,
      params: {
        termId: termId
      }
    })
  }
}

// 导出API对象
export default psyAnswer

// 为了向下兼容，同时导出各个方法
export const save = psyAnswer.save
export const batchDelete = psyAnswer.batchDelete
export const findPage = psyAnswer.findPage
export const findByCurrentUser = psyAnswer.findByCurrentUser
export const findByStudentAndTerm = psyAnswer.findByStudentAndTerm
export const saveAnswers = psyAnswer.saveAnswers 